local skel = fk.CreateSkill {
  name = "rmt__lieshu",
}

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.from and move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea <= Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end, Player.HistoryTurn) == 0 then return false end
    return player:hasSkill(skel.name) and target.phase == Player.Discard
  end,
  on_cost = function (self, event, target, player, data)
    local card_num = 0
    player.room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function (e)
      for _, move in ipairs(e.data) do
        if move.proposer == target and move.moveReason == fk.ReasonDiscard then
          card_num = card_num + #move.moveInfo
        end
      end
      return false
    end, Player.HistoryPhase)
    local tricks = {}
    for _, card in ipairs(Fk.cards) do
      if not table.contains(player.room.disabled_packs, card.package.name) and not card.is_derived then
        if card.type == Card.TypeTrick and card.sub_type ~= Card.SubtypeDelayedTrick
        and Fk:translate(card.trueName):len() == card_num then
          table.insertIfNeed(tricks, card.name)
        end
      end
    end
    if #tricks == 0 then
      event:setCostData(self, {extra_data = card_num})
      return true
    else
      local use = player.room:askToUseVirtualCard(player, {
        name = tricks,
        prompt = "#rmt__lieshu-viewas",
        skill_name = skel.name,
        cancelable = true,
        skip = true,
      })
      if use then
        event:setCostData(self, {extra_data = use})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local cost_data = event:getCostData(self).extra_data
    if type(cost_data) == "number" then
      player:drawCards(cost_data, skel.name)
    else
      player.room:useCard(cost_data)
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__lieshu"] = "烈戍",
  [":rmt__lieshu"] = "一名角色的弃牌阶段结束时，若你本回合失去过牌。你可以视为使用一张牌名字数为弃牌数的普通锦囊牌，若无，你摸等量牌。",

  ["#rmt__lieshu-viewas"] = "烈戍：你可以视为使用一张牌名字数为弃牌数的普通锦囊牌",

  ["$rmt__lieshu1"] = " ",
  ["$rmt__lieshu2"] = " ",
}

return skel
